package com.egao.common.manage.school;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.company.constants.CheckRecordConstants;
import com.egao.common.module.company.constants.CompanyConstants;
import com.egao.common.module.company.entity.vo.RecruitFormVO;
import com.egao.common.module.company.entity.vo.RecruitListVO;
import com.egao.common.module.company.service.RecruitService;
import com.egao.common.module.push.service.PositionMatchingService;
import com.egao.common.module.student.entity.vo.StudentExcelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 学校招聘管理
 * @author dl
 */
@Api(tags = "学校招聘管理")
@RestController
@RequestMapping("${api}")
public class SchoolRecruitController extends BaseController {

    @Autowired
    private RecruitService recruitService;

    @Autowired
    private PositionMatchingService positionMatchingService;

    @PreAuthorize("hasAuthority('school:recruit:list')")
    @ApiOperation(value = "招聘信息", notes = "未审核状态升序 创建时间倒序")
    @OperLog(value = "招聘信息", desc = "分页查询招聘信息")
    @ApiPageParam
    @GetMapping("/school/recruit/page")
    public PageResult<RecruitListVO> page(HttpServletRequest request) {
        PageParam<RecruitListVO> pageParam = new PageParam<>(request);
        return new PageResult<>(recruitService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:recruit:save')")
    @OperLog(value = "添加招聘信息", desc = "添加招聘信息", result = true)
    @ApiOperation(value = "添加招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitFormVO", value = "recruitFormVO", required = true, dataType = "RecruitFormVO")
    })
    @PostMapping("/school/recruit/save")
    public JsonResult save(@Valid @RequestBody RecruitFormVO recruitFormVO) {
        recruitFormVO.setCreateBy(getLoginUserId());
        //招聘信息为单位发布
        recruitFormVO.setAddType(CompanyConstants.BUSINESS_TYPE_SCHOOL);
        return recruitService.saveRecruit(recruitFormVO);
    }

    @PreAuthorize("hasAuthority('school:recruit:check')")
    @OperLog(value = "审核招聘信息", desc = "审核招聘信息", result = true)
    @ApiOperation(value = "审核招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitFormVO", value = "recruitFormVO", required = true, dataType = "RecruitFormVO")
    })
    @PutMapping("/school/recruit/check")
    public JsonResult check(@Valid @RequestBody RecruitFormVO recruitFormVO) {
        recruitFormVO.setCheckUserId(getLoginUserId());
        return recruitService.checkRecruit(recruitFormVO);
    }

    @PreAuthorize("hasAuthority('school:recruit:delete')")
    @OperLog(value = "删除招聘信息", desc = "删除招聘信息", result = true)
    @ApiOperation(value = "删除招聘信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recruitFormVO", value = "recruitFormVO", required = true, dataType = "RecruitFormVO")
    })
    @PutMapping("/school/recruit/delete")
    public JsonResult deleteRecruit(@RequestBody RecruitFormVO recruitFormVO) {
        return recruitService.removeRecruit(recruitFormVO);
    }

    @PreAuthorize("hasAuthority('school:recruit:detail')")
    @ApiOperation(value = "获取招聘信息详情")
    @OperLog(value = "获取招聘信息详情", desc = "获取招聘信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "招聘信息id", required = true, dataType = "int")
    })
    @GetMapping("/school/recruit/detail")
    public JsonResult detail(Integer id) {
        return recruitService.detail(id);
    }

    @PreAuthorize("hasAuthority('school:recruit:export')")
    @ApiOperation(value = "获取招聘信息详情")
    @OperLog(value = "获取招聘信息详情", desc = "获取招聘信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "招聘信息id", required = true, dataType = "int")
    })
    @GetMapping("/school/recruit/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        PageParam<RecruitFormVO> pageParam = new PageParam<>(request);
        recruitService.exportRecruit(pageParam, response);
    }

    @PreAuthorize("hasAuthority('school:recruit:detail')")
    @ApiOperation(value = "获取职位推荐统计")
    @OperLog(value = "获取招聘信息详情", desc = "获取招聘信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "招聘信息id", required = true, dataType = "int")
    })
    @GetMapping("/school/recruit/push-total")
    public JsonResult getTotal(Integer id) throws IOException {
        return positionMatchingService.getTotal(id, CheckRecordConstants.CHECK_RECRUIT);
    }
}
